package edu.friday.common.security.service;


import edu.friday.common.security.LoginUser;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

@Component
public class SysLoginService {

    @Autowired
    private MyTokenService myTokenService;

    @Resource
    private AuthenticationManager authenticationManager;

    public String login(String username, String password,String code,String uuid) {
        Authentication authentication = null;
        try {
            authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));

        }catch (Exception e) {
            throw new BadCredentialsException("Invalid username or password");
        }
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();

        return myTokenService.createToken(loginUser);

    }
}
